package cn.com.fuhuitong.main.order.ui.activity

import android.os.Bundle
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentStatePagerAdapter
import androidx.viewpager.widget.ViewPager
import cn.com.fuhuitong.app.AppConstance
import cn.com.fuhuitong.aroute.ARouteConstance
import cn.com.fuhuitong.main.base.ViewModeActivity
import cn.com.fuhuitong.main.order.adapter.OVT
import cn.com.fuhuitong.main.order.adapter.OrderTabAdapter
import cn.com.fuhuitong.main.order.ui.fragment.OrderListFragment
import cn.com.fuhuitong.main.order.vm.OrderViewModel
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.vlayout.layout.GridLayoutHelper
import cn.com.fuhuitong.R
import kotlinx.android.synthetic.main.activity_order_list.*

@Route(path = ARouteConstance.ACTIVITY_URL_ORDER_LIST)
class OrderListActivity : ViewModeActivity<OrderViewModel>() {

    @Autowired(name = AppConstance.BUNDLE_ORDER_STATUS)
    @JvmField
    var orderStatus: Int = 0

    private val orderTabData = mutableListOf("全部", "待付款", "待收货", "已完成")

    private lateinit var orderTabAdapter: OrderTabAdapter

    override fun viewModel(): OrderViewModel = createViewModel()

    override val layoutResId: Int
        get() = R.layout.activity_order_list

    override fun initView() {

        image_order_list_back.setOnClickListener {
            this.finish()
        }

        this.initViewRecyclerAdapter(recycler_order_tab, arrayOf(OVT.ORDER_TAB.ordinal))

        delegateAdapter.addAdapter(OrderTabAdapter(
            applicationContext, GridLayoutHelper(orderTabData.size), orderTabData
        ).apply {
            orderTabAdapter = this
        })

        view_pager.adapter = object : FragmentStatePagerAdapter(
            supportFragmentManager, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT
        ) {
            override fun getItem(position: Int): Fragment = OrderListFragment.newInstance(position)

            override fun getCount(): Int = orderTabData.size

            override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
                //super.destroyItem(container, position, `object`)
            }
        }
        view_pager.setScrollAnim(false)
        view_pager.setNoScroll(false)


        orderTabAdapter.selectedPosition = orderStatus
        view_pager.currentItem = orderStatus

        view_pager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrollStateChanged(state: Int) = Unit

            override fun onPageScrolled(
                position: Int, positionOffset: Float, positionOffsetPixels: Int
            ) = Unit

            override fun onPageSelected(position: Int) {
                orderTabAdapter.selectedPosition = position
            }
        })

        orderTabAdapter.setOnItemClickListener { _, i, _ ->
            orderTabAdapter.selectedPosition = i
            view_pager.currentItem = i
        }
    }


}